package com.kestrel.space.core.controller;

import com.kestrel.space.common.controller.BaseController;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.common.utils.Result;
import com.kestrel.space.core.model.MyFile;
import com.kestrel.space.core.query.MyFileQuery;
import com.kestrel.space.core.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author: kestrel
 * @Version:1.0
 * @Date: 2024/8/21
 */
@RestController
@RequestMapping("file")
public class FileController extends BaseController {
    @Autowired
    private FileService fileService;

    /**
     * 增删改查全表（内部）
     * @param query
     * @return
     */
    @RequestMapping("listContent")
    public Result listContent(@RequestBody MyFileQuery query){
        return Result.list(fileService.listContent(query, new LargePage(query.getPage(), query.getRows())));
    }
    @RequestMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody MyFile file){
        return Result.ok(fileService.saveOrUpdate(file));
    }
    @RequestMapping("batchSaveOrUpdate")
    public Result batchSaveOrUpdate(@RequestBody List<MyFile> fileList){
        return Result.ok(fileService.batchSaveOrUpdate(fileList));
    }

    @RequestMapping("previewFile")
    public Result previewFile(@RequestParam Long id){
        return Result.ok(fileService.previewFile(id));
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @RequestMapping("upload")
    public Result uploadFile(@RequestPart("file") MultipartFile file, @RequestParam String osUrl, @RequestParam String md5) throws Exception {
        String filename = file.getOriginalFilename();
        String type = "";
        if (filename!= null) {
            int lastDotIndex = filename.lastIndexOf('.');
            if (lastDotIndex > 0 && lastDotIndex < filename.length() - 1) {
                type = filename.substring(lastDotIndex + 1);
            }
        }
        return Result.ok(fileService.uploadFile(file, type, osUrl, getUser(), md5));
    }

    /**
     * 下载文件
     * @param id
     */
    @RequestMapping("download")
    public void downloadFile(@RequestParam Long id) {
        fileService.downloadFile(id, request, response);
    }

    /**
     * 逻辑删除文件
     */
    @RequestMapping("delete")
    public Result deleteFile(@RequestParam Long id){
        return Result.ok(fileService.deleteFile(id));
    }

    /**
     * 文件转 pdf 同步至博客系统
     */
    @RequestMapping("synchronization")
    public Result synchronizationFile(@RequestParam Long id) throws Exception {
        return Result.ok(fileService.synchronizationFile(id, getUser()));
    }

    /**
     * 列出当前层级文件
     * @return
     */
    @RequestMapping("listLevelFiles")
    public Result listLevelFiles(@RequestParam String osUrl){
        return Result.ok();
    }

    /**
     * 列出组织内部文件
     * @param query
     * @return
     */
    @RequestMapping("listOrganizationFiles")
    public Result listOrganizationFiles(@RequestBody MyFileQuery query){
        return Result.ok(fileService.listOrganizationFiles(query));
    }

    /**
     * 把文件分配/取消分配给组织
     * 个人网盘进行分配
     * 组织中进行取消分配（超管和上传者可以取消）
     * @param fileId
     * @param organization
     * @return
     */
    @RequestMapping("allocate")
    public Result allocate(@RequestParam Long fileId, @RequestParam Long organization, @RequestParam boolean stage){
        return Result.ok(fileService.allocate(fileId, organization, stage));
    }

    /**
     * 展示组织中我的文件编号
     * @return
     * @throws Exception
     */
    @RequestMapping("listMyFilesInOrg")
    public Result listMyFilesInOrg(@RequestParam Long organizationId) throws Exception {
        return Result.ok(fileService.listMyFilesInOrg(getUser(), organizationId));
    }
}
